/*
 * Copyright (c) United Binary LLC.  All rights reserved.
 * 
 * This code is licensed under the MIT License
 * 
 * SEE: http://harnessit.codeplex.com/license
 * 
 */
using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using UnitedBinary.Core.Windows.Forms;

using UnitedBinary.HarnessIt.SharedTypes;

namespace UnitedBinary.HarnessIt
{
	/// <summary>
	///		The SaveLogForm form is used to store then save the rich text output.
	///		Most likely, this form will NEVER be seen by the user.
	/// </summary>
	internal class SaveLogForm : System.Windows.Forms.Form
	{
		#region Member Variables
		private System.Windows.Forms.Label labelDesc;
		private System.Windows.Forms.Label LabelOutputFile;
		private System.Windows.Forms.RichTextBox richTextBoxReport;

		private RichTextComposition rtCompReportDetails;
		private RichTextComposition rtCompLog;
		private RichTextComposition rtCompFailures;
		private RichTextComposition rtCompTrace;

		private string filename = "";
		private System.Windows.Forms.Button buttonClose;

		/// <summary>
		///		Any exceptions encountered during file processing are caught here.
		/// </summary>
		internal Exception e;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		#endregion

		#region Construction and Destruction
		/// <summary>
		///		Intializing Constructor.
		/// </summary>
		/// <param name="file">The file to save the log to.</param>
		/// <param name="comps">A RichTextComposition array which constitutes the report.</param>
		internal SaveLogForm(string file, RichTextComposition rtCompReportDetails, RichTextComposition rtCompLog, RichTextComposition rtCompFailures, RichTextComposition rtCompTrace)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// Add any constructor code after InitializeComponent call
			//
			this.rtCompFailures = rtCompFailures;
			this.rtCompReportDetails = rtCompReportDetails;
			this.rtCompLog = rtCompLog;
			this.rtCompTrace = rtCompTrace;

			filename = file;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.labelDesc = new System.Windows.Forms.Label();
			this.richTextBoxReport = new System.Windows.Forms.RichTextBox();
			this.LabelOutputFile = new System.Windows.Forms.Label();
			this.buttonClose = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// labelDesc
			// 
			this.labelDesc.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.labelDesc.Location = new System.Drawing.Point(8, 12);
			this.labelDesc.Name = "labelDesc";
			this.labelDesc.Size = new System.Drawing.Size(328, 23);
			this.labelDesc.TabIndex = 0;
			this.labelDesc.Text = "Please wait while we generate and save the test report.";
			// 
			// richTextBoxReport
			// 
			this.richTextBoxReport.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.richTextBoxReport.Location = new System.Drawing.Point(8, 104);
			this.richTextBoxReport.Name = "richTextBoxReport";
			this.richTextBoxReport.ReadOnly = true;
			this.richTextBoxReport.Size = new System.Drawing.Size(328, 212);
			this.richTextBoxReport.TabIndex = 1;
			this.richTextBoxReport.Text = "";
			this.richTextBoxReport.WordWrap = false;
			// 
			// LabelOutputFile
			// 
			this.LabelOutputFile.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.LabelOutputFile.Location = new System.Drawing.Point(8, 44);
			this.LabelOutputFile.Name = "LabelOutputFile";
			this.LabelOutputFile.Size = new System.Drawing.Size(328, 48);
			this.LabelOutputFile.TabIndex = 2;
			this.LabelOutputFile.Text = "SAVE FILE TO...";
			// 
			// buttonClose
			// 
			this.buttonClose.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.buttonClose.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.buttonClose.Location = new System.Drawing.Point(260, 324);
			this.buttonClose.Name = "buttonClose";
			this.buttonClose.TabIndex = 3;
			this.buttonClose.Text = "OK";
			this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
			// 
			// SaveLogForm
			// 
			this.AcceptButton = this.buttonClose;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(344, 353);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.buttonClose,
																		  this.LabelOutputFile,
																		  this.richTextBoxReport,
																		  this.labelDesc});
			this.Name = "SaveLogForm";
			this.ShowInTaskbar = false;
			this.Text = "HarnessIt - Generating Report";
			this.Load += new System.EventHandler(this.OnLoad);
			this.VisibleChanged += new System.EventHandler(this.OnVisibleChanged);
			this.Activated += new System.EventHandler(this.OnActivated);
			this.Enter += new System.EventHandler(this.OnEnter);
			this.ResumeLayout(false);

		}
		#endregion

		#region Test Log File Generation
		/// <summary>
		///		OnLoad event handler.  We'll just render the report to our rich text box 
		///		and save it here before the user ever sees the form.
		/// </summary>
		/// <param name="sender">Standard sender object</param>
		/// <param name="e">Standard EventArgs object.</param>
		private void OnLoad(object sender, System.EventArgs e)
		{
			try
			{
				LabelOutputFile.Text = "Saving to file: \n" + filename;

				RichTextComposition report = new RichTextComposition();
				report.RichTextBox = richTextBoxReport;
				report.OwnsRichTextBox = true;
				Font labelFont = new System.Drawing.Font("Tahoma",10,System.Drawing.FontStyle.Bold);
				report.Font = labelFont;

				string header = "Report generated on " + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToLongTimeString() + " by HarnessIt v" + Version + ".";
				RichTextString dateRTS = new RichTextString(header,Color.DarkGray,labelFont);
				report.AddLine(dateRTS);
				report.AddLine("");

				foreach(RichTextString s in rtCompReportDetails.Strings)
				{
					report.Add(s);
				}

				report.AddLine("_______________________________________");
				report.AddLine(new RichTextString("Test Output (failures only):\n",Color.DarkGreen,labelFont));

				foreach(RichTextString s in rtCompFailures.Strings)
				{
					report.Add(s);
				}

				report.AddLine("_______________________________________");
				report.AddLine(new RichTextString("Test Output (all):\n",Color.DarkGreen,labelFont));

				foreach(RichTextString s in rtCompLog.Strings)
				{
					report.Add(s);
				}

				report.AddLine("_______________________________________");
				report.AddLine(new RichTextString("Trace Output:\n",Color.DarkGreen,labelFont));

				foreach(RichTextString s in rtCompTrace.Strings)
				{
					report.Add(s);
				}

				report.AddLine("_______________________________________");

				report.Render();

				richTextBoxReport.SaveFile(filename);
			}
			catch(Exception exp)
			{
				// We need to store this excpetion rather than throwing it because
				// this form is executing in a different thread than the main form
				// (something like this) and the exception doesn't pop along the 
				// call stack there to be caught.
				this.e = exp;
			}

			finally
			{
				this.Close();
			}
		}
		#endregion

		#region Windows Forms event handling code
		private void buttonClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void OnVisibleChanged(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void OnActivated(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void OnEnter(object sender, System.EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region Properties

		private string Version
		{
			get
			{
				try
				{
					string basedir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
					Assembly A = new Assembly(basedir + "HarnessIt.exe");

					return A.Version;
				}
				catch(Exception e)
				{
					Debug.WriteLine("Error in Version: " + e.ToString());
					return "0.0.0.0";
				}
			}
		}
		#endregion
	}
}
